package com.ellwood.common.core.result;

import com.ellwood.common.core.enums.HttpCodeEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.io.Serial;
import java.io.Serializable;

/**
 * @ProjectName: Ellwood-framework-boot
 * @PackageName: com.ellwood.common.core.result
 * @ClassName: ApiResult
 * @Description: // 统一响应返回
 * @Author: Ellwood
 * @CreateDate: 2024/7/15 - 下午12:01
 * @Version: V1.0.0
 */
@Data
public class ApiResult<T> implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;

    @Schema(description = "响应码")
    private Integer code;
    @Schema(description = "响应信息")
    private String msg;
    @Schema(description = "响应数据")
    private T data;

    public ApiResult() {
        this.code = HttpCodeEnum.SUCCESS.getCode();
        this.msg = HttpCodeEnum.SUCCESS.getMsg();
    }

    public ApiResult(Integer code, T data) {
        this.code = code;
        this.data = data;
    }

    public ApiResult(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public ApiResult(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> ApiResult<T> errorResult(int code, String msg) {
        ApiResult<T> result = new ApiResult<>();
        return result.error(code, msg);
    }

    public static <T> ApiResult<T> okResult() {
        return new ApiResult<T>();
    }

    public static <T> ApiResult<T> okResult(int code, String msg) {
        ApiResult<T> result = new ApiResult<>();
        return result.ok(code, msg, null);
    }

    public static <T> ApiResult<T> okResult(T data) {
        ApiResult<T> result = setHttpCodeEnum(HttpCodeEnum.SUCCESS, HttpCodeEnum.SUCCESS.getMsg());
        if (data != null) {
            result.setData(data);
        }
        return result;
    }

    public static <T> ApiResult<T> Success(HttpCodeEnum httpCodeEnum) {
        return setHttpCodeEnum(httpCodeEnum, httpCodeEnum.getMsg());
    }

    public static <T> ApiResult<T> errorResult(HttpCodeEnum codeEnum) {
        return setHttpCodeEnum(codeEnum, codeEnum.getMsg());
    }

    private static <T> ApiResult<T> errorResult(HttpCodeEnum codeEnum, String msg) {
        return setHttpCodeEnum(codeEnum, msg);
    }

    public static <T> ApiResult<T> setHttpCodeEnum(HttpCodeEnum codeEnum) {
        return okResult(codeEnum.getCode(), codeEnum.getMsg());
    }

    private static <T> ApiResult<T> setHttpCodeEnum(HttpCodeEnum codeEnum, String msg) {
        return okResult(codeEnum.getCode(), msg);
    }

    public ApiResult<T> error(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
        return this;
    }

    public ApiResult<T> ok(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        return this;
    }

    public ApiResult<T> ok(Integer code, T data) {
        this.code = code;
        this.data = data;
        return this;
    }

    public ApiResult<T> ok(T data) {
        this.data = data;
        return this;
    }

}
